<?php
class PaymentOrder extends PaymentProcess{
	
	protected $_orderId;
	
	public function save(){
		$db = Yii::app()->db->beginTransaction();
		try {
			$billingId = $this->billingInfo->saveInfo();
		
			$order 					= new Order();
		
			$order->customer_id		=	$this->customer->id;
			$order->status			=	1;
			$order->created			=	time();
			$order->price			=	$this->item->price;
			$order->payment_type	=	$this->_processInfo['paymentType'];
			$order->level			=	$this->customer->level;
			$order->offer_id		=	$this->item->offerId;
			$order->item_id			=	$this->item->itemId;
			$order->billing_id		=	$billingId;
			$order->is_delete		=	0;
			$order->payment_log		=	$this->getPaymentCode();
			$order->save();
		
			$this->_orderId = $order->id;
			$customerMark			= new CustomerMark();
			$customerMark->customer_id = $this->customer->id;
			$customerMark->mark		= 'Payment';
			$customerMark->value	= $order->id;
			$customerMark->setMark();
			
			
			$offer = Yii::app()->offer;
			
			$customerMark			= new CustomerMark();
			$customerMark->customer_id = $this->customer->id;
			$customerMark->mark		= $offer->getOfferKey();
			$customerMark->value	= $this->item->offerId;
			$customerMark->setMark();
			
			Email::model()->updateAll(array('send'=>2),' offer_id =:offerId AND send = 0 ',array(':offerId'=> $this->item->offerId));
			
			$uac = $this->customer->uac;
			if (!empty($uac)){
				$report = new ReportOffer();
				$report->reportData(array(
						'uac'			=>	$uac,
						'offerId'		=>	$this->item->offerId,
						'price'			=>	$this->item->price,
						'level'			=>	$this->customer->level,
						'customerId'	=>	$this->customer->id,
				));
			}
		
			CustomerInfo::getSession()->setOfferLevel(2);
		
			$db->commit();
			
			$paymentInfo = new PaymentInfo();
			$paymentInfo->setPaymentCode($this->getPaymentCode())->deleteCache();
			
			
			
			$email = Yii::app()->sendgrid;
				
			
			//order Info 
			$orderInfo = new OrderInfo();
			$orderInfo->id = $this->_orderId;
			$orderInfo->customer_id = $this->customer->id;
			$emailOrderInfo = $orderInfo->getOrderInfo();
			
			$html = <<<orderInfo
							
			<table width="95%" cellspacing="0" border="0" style="margin: 0px auto;">
						<tr>
							<td>Order #</td>
							<td>Title</td>
							<td>Offer</td>
							<td>Price</td>
						</tr>
						<tr align="left"><td colspan="6"><hr></td></tr>
						<tr>
							<td>{$emailOrderInfo->id}</td>
							<td>{$emailOrderInfo->itemTable->title}</td>
							<td>{$emailOrderInfo->offerTable->name}</td>
							<td>$ {$emailOrderInfo->itemTable->price}</td>
						</tr>
		</table>
orderInfo;
			$offerInfo = Offer::model()->findAll(' is_delete = 0 and id != '.$this->item->offerId);
			
			$tempHtml = null;
			if ($offerInfo){
				$emailOffer = Yii::app()->offer;
				foreach ($offerInfo as $val){
					$emailOffer->setOfferKey($val->key);
					$logo = $emailOffer->getOfferInfo('logo');
					if (!empty($logo)){
						$tempHtml .= '<div><a href="http://dev.'.$val->name.'" ><img src=http://dev.'.$val->name.$emailOffer->getOfferInfo('base').'/images/'.$logo.' /></a></div>'."\n";
					}
				} 
			}
			
			$offerHtml = <<<offer
				<div>
					{$tempHtml}						
				</div>	
offer;
			
			//order Info
			$offer->setDomain();
			//$offerMessage = 'With your purchase you have unlocked some of our other great deals that are only available to '.$offer->getOfferInfo('domain').' customers.';
			$offerMessage = null;
			$email->setTitle('Your Order Confirmation');
			$email->addTo($this->customer->email);
			$email->setFrom($offer->getOfferInfo('fromEmail'));
			$email->setEmailTemplate('paymentemail',array(
					'{[OFFER_LOGO]}'	=> 'http://'.$offer->getOfferInfo('domain').$offer->getOfferInfo('base').'/images/'.$offer->getOfferInfo('logo'),
					'{[OFFER_DEAL]}'	=>	$offer->getOfferInfo('domain'),
					'{[OTHER_OFFER]}'	=>	null,
					'{[OTHER_OFFER_MESSAGE]}'	=>	$offerMessage,
					'{[ORDER_INFO]}'	=>	$html,
			));
			//$email->_debug = true;
			$email->send($this->customer->id);
			
		} catch (Exception $ex){
			$db->rollback();
		}
		
		return $this;
	}
	
	public function redirectOrder(){
		Yii::app()->session['paymentSuccess'] = true;
		Yii::app()->getRequest()->redirect(Yii::app()->createUrl('order-'.$this->_orderId));
	}
}